package com.cloud.cmt.cmtsso.controller;

import com.alibaba.fastjson.JSONObject;
import com.cloud.cmt.cmtsso.entity.UserInfo;
import com.cloud.cmt.cmtsso.service.impl.UserServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by hdwang on 2017/6/19.
 */
@Controller
@RequestMapping("/home")
public class HomeController {

    @Autowired
    UserServiceImpl userService;

    @RequestMapping("")
    public String index(HttpSession session, ModelMap map, HttpServletRequest request){
//        UserInfo user = (UserInfo) session.getAttribute("user");

        System.out.println(request.getUserPrincipal().getName());
        System.out.println(SecurityUtils.getSubject().getPrincipal());

        UserInfo loginUser = userService.findByName(request.getUserPrincipal().getName());
        System.out.println(JSONObject.toJSONString(loginUser));

        map.put("user",loginUser);
        return "home";
    }

    @RequestMapping("get")
    public @ResponseBody UserInfo getUserInfo(HttpSession session, HttpServletRequest request){
//        UserInfo user = (UserInfo) session.getAttribute("user");

        System.out.println(request.getUserPrincipal().getName());
        System.out.println(SecurityUtils.getSubject().getPrincipal());

        UserInfo loginUser = userService.findByName(request.getUserPrincipal().getName());
        System.out.println();

        return loginUser;
    }


}
